package de.shortys.myhome.api.view;

import static de.java.shortys.utils.view.ViewUtils.getIconFromString;
import static javax.swing.SwingConstants.CENTER;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.util.Observable;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;
import de.shortys.myhome.api.view.components.JImagePanel;
import de.shortys.myhome.api.view.event.listener.ModuleCloseListener;

public class AbstractModuleViewHead extends AbstractComponent {

	private JPanel leftPane;

	private JPanel centerPane;

	private JPanel rightPane;

	private JLabel titleLabel;

	private JLabel closeLabel;

	@Override
	public void update(Observable o, Object arg) {
		// Empty
	}

	public void setTitle(String title) {
		titleLabel.setText(title);
	}

	public void addModuleCloseListener(ModuleCloseListener listener) {
		closeLabel.addMouseListener(listener);
	}

	public void setIcon(ImageIcon icon) {
		titleLabel.setIcon(icon);
	}

	@Override
	protected void buildComponents() {
		getContentPane().setOpaque(false);
		getContentPane().setLayout(new BorderLayout());
		buildLeftPane();
		buildCenterPane();
		buildRightPane();
	}

	private void buildRightPane() {
		rightPane = new JImagePanel(getIconFromString("de/shortys/myhome/api/view/images/moduleview_head_right.png"));
		rightPane.setLayout(new BorderLayout());
		rightPane.setOpaque(false);
		rightPane.setPreferredSize(new Dimension(9, 30));
		getContentPane().add(BorderLayout.EAST, rightPane);
	}

	private void buildCenterPane() {
		centerPane = new JImagePanel(getIconFromString("de/shortys/myhome/api/view/images/moduleview_head_center.png"));
		centerPane.setLayout(new MigLayout("", "0[grow]", "0[grow]"));
		centerPane.setOpaque(false);

		titleLabel = new JLabel();
		centerPane.add(titleLabel, "dock west");

		closeLabel = new JLabel("x");
		closeLabel.setPreferredSize(new Dimension(20, 20));
		closeLabel.setHorizontalAlignment(CENTER);
		closeLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		centerPane.add(closeLabel, "dock east");

		getContentPane().add(centerPane);
	}

	public void setClosable(boolean closable) {
		closeLabel.setVisible(closable);
	}

	private void buildLeftPane() {
		leftPane = new JImagePanel(getIconFromString("de/shortys/myhome/api/view/images/moduleview_head_left.png"));
		leftPane.setLayout(new BorderLayout());
		leftPane.setOpaque(false);
		leftPane.setPreferredSize(new Dimension(9, 30));
		getContentPane().add(BorderLayout.WEST, leftPane);
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
}
